<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/12 0012
 * Time: 9:49
 */

namespace app\api\controller;
use think\Controller;
use think\Db;

class Web extends Controller
{
    //课程分类
    public function cate()
    {
        $list=db('pc_cate')->field('id,cate')->order('create_time desc')->select();
        if(!empty($list)){
              $re['list']=$list;
              $re['kc']=db('pc_xueke x')
                  ->join('pc_cate p','p.id=x.cate_id','left')
                  ->where('x.cate_id',$list[0]['id'])->field('x.id,x.icon,x.title,x.price,p.cate as cate_title')
                  ->order('x.sort asc')
                  ->limit(6)
                  ->select();

            return apiSuccess($re);
        }else{
            return apiSuccess([],'暂无数据');
        }
    }

    //课程
   public function get_kc()
   {
       $id=input('post.id/d');
       $list=db('pc_xueke x')
           ->join('pc_cate p','p.id=x.cate_id','left')
           ->where('x.cate_id',$id)->field('x.id,x.icon,x.title,x.price,p.cate as cate_title')
           ->order('x.sort asc')
           ->limit(6)
           ->select();
       if(!empty($list)){
           return apiSuccess($list);
       }else{
           return apiSuccess([],'暂无数据');
       }
   }

   //师资导师
   public function  teacher()
   {
       $page=input('post.page/d','1');
       $offset=($page-1)*4;
       $list=db('lecturer')->limit($offset,4)->order('sort asc')->field('id,photo,title,summary')->select();
       if(!empty($list)){
           return apiSuccess($list);
       }else{
           return apiSuccess([],'暂无数据');
       }
   }
   //老师详情资料
   public function teacher_detail()
   {
       $id=input('id/d');
       if(!$id)return apiError('非法操作','404');
       $row=db('lecturer')->where('id',$id)->field('updated,created',true)->find();
       return apiSuccess($row);
   }

   public function company_short()
   {
       $row=db('company')->where('id',1)->value('short');
       if(!empty($row)){
           return apiSuccess($row);
       }else{
           return apiSuccess([],'暂无数据');
       }
   }
   public function about_us()
    {
        $row=db('company')->where('id',1)->value('about_us');
        if(!empty($row)){
            return apiSuccess($row);
        }else{
            return apiSuccess([],'暂无数据');
        }
    }
   public function  play_video()
    {
        $id=input('post.kc_id/d');
        $list=db('pc_video')->where('xueke_id',$id)->order('sort asc')->field('id,title,play_url,time,is_xz')->select();
        if(!empty($list)){
            $data['count']=count($list);
            $data['list']=$list;
            $data['price']=db('pc_xueke')->where('id',$id)->value('price');
            return apiSuccess($data);
        }else{
            return apiSuccess([],'暂无数据');
        }
    }

    //产品介绍
   public function  kc_list()
    {
        $list=db('pc_xueke x')
            ->join('pc_cate p','p.id=x.cate_id','left')
            ->field('x.id,x.icon,x.title,x.price,p.cate as cate_title')
            ->order('x.sort asc')
            ->paginate(8);
        if(!empty($list)){
            return apiSuccess($list);
        }else{
            return apiSuccess([],'暂无数据');
        }
    }

    //老师列表
   public function  tc_list()
  {
      $list = db('lecturer')->field('id,title,photo')
          ->order('sort asc')->paginate(8)
          ->each(function ($item, $key) {
              $item['kc'] = db('pc_xueke')->orderRaw('rand()')->field('title')->limit(3)->select();
             return $item;
          });
      if(!empty($list)){
          return apiSuccess($list);
      }else{
          return apiSuccess([],'暂无数据');
      }
  }

  //新闻列表
    public function  news_list()
{

    $page_count=!empty(input('post.page_count'))?input('post.page_count/d'):'10';
    $list=db('pc_news')->field('id,title,create_time')->order('create_time desc')->paginate($page_count);;
    if(!empty($list)){
        return apiSuccess($list);
    }else{
        return apiSuccess([],'暂无数据');
    }
}
  //新闻详情
   public function  news_detail()
{
    $id=input('post.id/d');
    $row=db('pc_news')->where('id',$id)->find();
    $row['author']='运营';
    $row['last']=db('pc_news')->where('create_time','<',$row['create_time'])->order('create_time desc')->field('id,title')->find();
    $row['next']=db('pc_news')->where('create_time','>',$row['create_time'])->order('create_time desc')->field('id,title')->find();
    $row['create_time']=date('Y-m-d',$row['create_time']);
    if(!empty($row)){
        return apiSuccess($row);
    }else{
        return apiSuccess([],'暂无数据');
    }
}

   public function  get_banner()
{
    $list=db('pc_banner')->where(['status'=>1])->order('create_time desc')->field('id,url')->select();
    if(!$list)return apiSuccess([],'暂无banner');
    return apiSuccess($list,'获取成功');
}

  //获取参数
    public function get_config(){
        $data=[];
        $data['icp']=db('set')->where('set_key','ICPB')->value('set_val');
        $data['andriod_href']=db('set')->where('set_key','ANDRIOD_HREF')->value('set_val');
        $data['ios_href']=db('set')->where('set_key','IOS_HRER')->value('set_val');
        $data['saoma']=db('set')->where('set_key','SAOMA')->value('set_val');
        $data['sub']=db('set')->where('set_key','XSZX')->value('set_val');
        $data['andriod_img']=db('set')->where('set_key','ANDRIOD_IMG')->value('set_val');
        $data['ios_img']=db('set')->where('set_key','IOS_IMG')->value('set_val');
        $data['opera']=db('set')->where('set_key','OPERA')->value('set_val');
        $data['kefu']=db('set')->where('set_key','SHORT_MSG_OPEN')->value('set_val');
        $data['kefu1']=db('set')->where('set_key','KEFU')->value('set_val');
        return apiSuccess($data,'获取成功');
    }

    public function test0301(){
        $ups['q_origin'] = null;
        $data = Db::name('tk_single_bak2')->where('q_origin',1)->update($ups);

        dump($data);exit;

    }



}